package gestionedt.models;

import gestionedt.controllers.PrestationJpaController;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 *
 */
@Entity
@Table(name = "groupe")
@NamedQueries({@NamedQuery(name = "Groupe.findAll",
                           query = "SELECT g FROM Groupe g"),
               @NamedQuery(name = "Groupe.findById",
                           query = "SELECT g FROM Groupe g WHERE g.id = :id"),
               @NamedQuery(name = "Groupe.findByLibelle",
                           query = "SELECT g FROM Groupe g WHERE g.libelle = :libelle"),
               @NamedQuery(name = "Groupe.findByEffectif",
                           query = "SELECT g FROM Groupe g WHERE g.effectif = :effectif")})
public class Groupe implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "libelle")
    private String libelle;
    @Column(name = "effectif")
    private Integer effectif;
    @JoinTable(name = "inscription_module", joinColumns = {@JoinColumn(name = "idGroupe", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "idModule", referencedColumnName = "id")})
    @ManyToMany
    private Collection<Module> moduleCollection;
    @JoinTable(name = "indisponibilite_groupe", joinColumns = {@JoinColumn(name = "idGroupe", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "numeroCreneau", referencedColumnName = "numero")})
    @ManyToMany
    private Collection<Creneau> creneauCollection;
    @JoinTable(name = "groupes_par_prestation", joinColumns = {@JoinColumn(name = "idGroupe", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "idPrestation", referencedColumnName = "id")})
    @ManyToMany
    private Collection<Prestation> prestationCollection;

    public Groupe() {
    }

    public Groupe(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public Integer getEffectif() {
        return effectif;
    }

    public void setEffectif(Integer effectif) {
        this.effectif = effectif;
    }

    public Collection<Module> getModules() {
        return moduleCollection;
    }

    public void setModules(Collection<Module> moduleCollection) {
        this.moduleCollection = moduleCollection;
    }

    public Collection<Creneau> getIndisponibilites() {
        return creneauCollection;
    }

    public void setIndisponibilites(Collection<Creneau> creneauCollection) {
        this.creneauCollection = creneauCollection;
    }

    public boolean estIndispo(Creneau creneau) {
        return creneauCollection.contains(creneau);
    }

    public boolean estDispo(Creneau creneau) {
        return !estIndispo(creneau);
    }

    public Collection<Prestation> getPrestationsSuivies() {
        return prestationCollection;
    }

    public void setPrestationsSuivies(Collection<Prestation> prestationCollection) {
        this.prestationCollection = prestationCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Groupe)) {
            return false;
        }
        Groupe other = (Groupe) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getLibelle();
    }

    /**
     * Indique si le groupe suit une prestation ou non
     * durant le créneau passé en paramètre.
     * @param creneau
     * @return
     */
    public boolean suitPrestationLe(Creneau creneau) {
        PrestationJpaController ctPrest = new PrestationJpaController();
        return ctPrest.suitPrestation(this, creneau);
    }

}
